<?php

namespace api\modules\v1\controllers\rbac;

use Yii;
use api\controllers\OnAuthController;
use common\models\rbac\Action;
use common\models\rbac\Menu;
use common\models\rbac\Module;
use common\helpers\CommonHelper;
use api\modules\v1\forms\rbac\ActionForm;

/**
 * Class ActionController
 * @package api\modules\v1\controllers\rbac
 */
class ActionController extends OnAuthController
{
    /**
     * @var Action
     */
    public $modelClass = Action::class;


    /**
     * @api {get} /v1/rbac/action/list 动作列表
     * @apiName list
     * @apiGroup Action
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   动作列表
     * @apiSuccess {Number}  data.list.id                动作ID
     * @apiSuccess {Number}  data.list.menu_id           菜单ID
     * @apiSuccess {Number}  data.list.module_id         模块ID
     * @apiSuccess {String}  data.list.name              动作名称
     * @apiSuccess {String}  data.list.menu_name         菜单名称
     * @apiSuccess {String}  data.list.module_name       模块名称
     * @apiSuccess {String}  data.list.flag              动作路由
     * @apiSuccess {Number}  data.list.sort              排序
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     * @apiSuccess {String}  data.list.created_at_str    创建时间
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->action->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->action->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/action/info 动作详情
     * @apiName info
     * @apiGroup Action
     *
     * @apiDescription 新增ID不传，编辑必传ID
     *
     * @apiParam {Number} id                        动作ID【选填】
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Number}  data.id                动作ID
     * @apiSuccess {Number}  data.menu_id           菜单ID
     * @apiSuccess {Number}  data.module_id         模块ID
     * @apiSuccess {String}  data.name              动作名称
     * @apiSuccess {String}  data.flag              动作路由
     * @apiSuccess {Number}  data.sort              排序
     * @apiSuccess {Number}  data.status            状态
     * @apiSuccess {Array}   data.module_list       全部模块列表【循环】
     * @apiSuccess {Number}  data.module_list.id    模块ID
     * @apiSuccess {String}  data.module_list.name  模块名
     * @apiSuccess {Object}  data.menu_list         全部菜单列表【循环  module_id => []】
     * @apiSuccess {Number}  data.menu_list.id      菜单ID
     * @apiSuccess {String}  data.menu_list.name    菜单名
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 动作信息
        $action = Yii::$app->services->action->findById($id);

        // 模块列表
        $moduleList = Yii::$app->services->module->findList([
            'select' => ['id', 'name'],
            'status' => Module::STATUS_ENABLE,
        ]);

        // 菜单列表
        $menuList = Yii::$app->services->menu->findList(['status' => Menu::STATUS_ENABLE]);

        // 菜单列表格式化
        $checkId = $action ? $action->menu_id : 0;
        $menuListFormat = Yii::$app->services->menu->getListModuleFormat($menuList, 0, $checkId);

        return [
            'id' => $action->id ?? 0,
            'menu_id' => $action->menu_id ?? 0,
            'module_id' => $action->module_id ?? 0,
            'name' => $action->name ?? '',
            'flag' => $action->flag ?? '',
            'status' => $action->status ?? Action::STATUS_ENABLE,
            'sort' => $action->sort ?? 0,
            'module_list' => $moduleList,
            'menu_list' => $menuListFormat,
        ];
    }

    /**
     * @api {post} /v1/rbac/action/create 新增动作
     * @apiName create
     * @apiGroup Action
     *
     * @apiParam {String} name              动作名称
     * @apiParam {Number} module_id         模块ID
     * @apiParam {Number} menu_id           菜单ID
     * @apiParam {String} flag              动作路由
     * @apiParam {Number} sort              排序【选填，不能小于0】
     * @apiParam {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionCreateAction()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ActionForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->action->create($model);
    }

    /**
     * @api {post} /v1/rbac/action/update 编辑动作
     * @apiName update
     * @apiGroup Action
     *
     * @apiParam {Number} id                动作ID
     * @apiParam {String} name              动作名称
     * @apiParam {Number} module_id         模块ID
     * @apiParam {Number} menu_id           菜单ID
     * @apiParam {String} flag              动作路由
     * @apiParam {Number} sort              排序【选填，不能小于0】
     * @apiParam {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionUpdateAction()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ActionForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->action->edit($model);
    }

    /**
     * @api {post} /v1/rbac/action/delete 删除动作
     * @apiName delete
     * @apiGroup Action
     *
     * @apiParam {Number} id                动作ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionDeleteAction()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new ActionForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->action->delete($model);
    }

}
